#include <log4cxx/logger.h>

#include "CnfSvrDBHelper.h"

using namespace log4cxx;

static LoggerPtr g_logger = Logger::getLogger("CnfSvrDBHelper");

int db_get_all_cnf(MYSQL * mysql, NS_CNF & ns, list<MDS_CNF> & l_mds, list<DS_CNF> & l_ds) {
	if (NULL == mysql) {
		LOG4CXX_ERROR(g_logger, "in get all cnf, invalid mysql conn");
		return -1;
	}
	string sql = "select server_ip, server_port, status, thread_num, socket_conn_timeout, socket_idle_timeout, "
			"socket_send_timeout, socket_recv_timeout, socket_max_request_size, socket_max_response_size, "
			"client_conn_pool_size, log_cnf_file, root_data_path, create_time from ns_cnf";
	int ret = mysql_real_query(mysql, sql.c_str(), sql.size());
	if (0 != ret) {
			LOG4CXX_ERROR(g_logger, "get all cnf error, sql:" << sql << ", errmsg:" << mysql_error(mysql));
			return -2;
	}
 	MYSQL_RES  * mysql_res1 = mysql_store_result(mysql);
	MYSQL_ROW row1;
	if (NULL != (row1 = mysql_fetch_row(mysql_res1))) {
			ns.ip = row1[0];
			ns.port = atoi(row1[1]);
			ns.status = atoi(row1[2]);
			ns.thread_num = atoi(row1[3]);
			ns.socket_conn_timeout = atoi(row1[4]);
			ns.socket_idle_timeout = atoi(row1[5]);
			ns.socket_send_timeout = atoi(row1[6]);
			ns.socket_recv_timeout = atoi(row1[7]);
			ns.socket_max_request_size = atoi(row1[8]);
			ns.socket_max_response_size = atoi(row1[9]);
			ns.client_conn_pool_size = atoi(row1[10]);
			ns.log_cnf_file = row1[11];
			ns.root_data_path = row1[12];
			struct tm ttm;
			strptime(row1[13], "%Y-%m-%d %H:%M:%S", &ttm);
			ns.create_time = mktime(&ttm);
        }
	mysql_free_result(mysql_res1);


	sql = "select server_ip, server_port, server_id, weight, status, thread_num, socket_conn_timeout, socket_idle_timeout, "
		"socket_send_timeout, socket_recv_timeout, socket_max_request_size, socket_max_response_size, "
		"client_conn_pool_size, log_cnf_file, root_data_path, create_time from mds_cnf";
	ret = mysql_real_query(mysql, sql.c_str(), sql.size());
	if (0 != ret) {
			LOG4CXX_ERROR(g_logger, "get all cnf error, sql:" << sql << ", errmsg:" << mysql_error(mysql));
			return -2;
	}

 	MYSQL_RES  * mysql_res2 = mysql_store_result(mysql);
	MYSQL_ROW row2;
	while (NULL != (row2 = mysql_fetch_row(mysql_res2))) {
			MDS_CNF mds;
			mds.ip = row2[0];
			mds.port = atoi(row2[1]);
			mds.id = atoi(row2[2]);
			mds.weight = atoi(row2[3]);
			mds.status = atoi(row2[4]);
			mds.thread_num = atoi(row2[5]);
                        mds.socket_conn_timeout = atoi(row2[6]);
                        mds.socket_idle_timeout = atoi(row2[7]);
                        mds.socket_send_timeout = atoi(row2[8]);
                        mds.socket_recv_timeout = atoi(row2[9]);
                        mds.socket_max_request_size = atoi(row2[10]);
                        mds.socket_max_response_size = atoi(row2[11]);
                        mds.client_conn_pool_size = atoi(row2[12]);
                        mds.log_cnf_file = row2[13];
                        mds.root_data_path = row2[14];
			struct tm ttm;
			strptime(row2[15], "%Y-%m-%d %H:%M:%S", &ttm);
			mds.create_time = mktime(&ttm);
			l_mds.push_back(mds);
        }
	mysql_free_result(mysql_res2);


	sql = "select server_ip, server_port, server_id, read_weight, write_weight, status, mds_id, thread_num, "
		"socket_conn_timeout, socket_idle_timeout, socket_send_timeout, socket_recv_timeout, socket_max_request_size, "
		"socket_max_response_size, client_conn_pool_size, log_cnf_file, root_data_path, create_time from ds_cnf";
	ret = mysql_real_query(mysql, sql.c_str(), sql.size());
	if (0 != ret) {
			LOG4CXX_ERROR(g_logger, "get all cnf error, sql:" << sql << ", errmsg:" << mysql_error(mysql));
			return -2;
	}

 	MYSQL_RES  * mysql_res3 = mysql_store_result(mysql);
	MYSQL_ROW row3;
	while (NULL != (row3 = mysql_fetch_row(mysql_res3))) {
			DS_CNF ds;
			ds.ip = row3[0];
			ds.port = atoi(row3[1]);
			ds.id = atoi(row3[2]);
			ds.read_weight = atoi(row3[3]);
			ds.write_weight = atoi(row3[4]);
			ds.status = atoi(row3[5]);
			ds.mds_id = atoi(row3[6]);
			ds.thread_num = atoi(row3[7]);
                        ds.socket_conn_timeout = atoi(row3[8]);
                        ds.socket_idle_timeout = atoi(row3[9]);
                        ds.socket_send_timeout = atoi(row3[10]);
                        ds.socket_recv_timeout = atoi(row3[11]);
                        ds.socket_max_request_size = atoi(row3[12]);
                        ds.socket_max_response_size = atoi(row3[13]);
                        ds.client_conn_pool_size = atoi(row3[14]);
                        ds.log_cnf_file = row3[15];
                        ds.root_data_path = row3[16];
			struct tm ttm;
			strptime(row3[17], "%Y-%m-%d %H:%M:%S", &ttm);
			ds.create_time = mktime(&ttm);
			l_ds.push_back(ds);
        }
	mysql_free_result(mysql_res3);
	return 1;
}
